<?php
class Match {
		
	public $id;
	public $liga;
	public $kolo;
	public $domacin;
	public $gost;
	public $vreme;
	public $odigrana;
	public $highlights;

	// constructor
	
	public function __construct($i = 0) {
		$this->id = $i;
		if ($i != 0) {
			$n = $this->getfromdb($i);
			$this->liga = $n['liga'];
			$this->kolo = $n['kolo'];
			$this->domacin = new Tim($n['domacin']);
			$this->gost = new Tim($n['gost']);
			$this->vreme = date("H:i", strtotime($n['vreme']));
			$this->odigrana = $n['odigrana'];
			$this->getHighlights();
		}
	}
	
	// functions
	
	private function getfromdb($number) {
		$q = "SELECT liga, kolo, domacin, gost, vreme, odigrana FROM utakmica WHERE id = " . $number;
		$r = mysql_query("set names 'utf8'");
		$r = mysql_query($q);
		if (!mysql_num_rows($r)) throw new Exception("Ne postoji Utakmica za zadati ID!");
		return (mysql_fetch_array($r));
	}
	
	public function getHighlights() {
		$q = "SELECT id FROM videos WHERE relatedID = " . $this->id;
		$r = mysql_query("set names 'utf8'");
		$r = mysql_query($q);
		if (!mysql_num_rows($r)) $this->highlights = array();
		else {
			while($n = mysql_fetch_array($r)) {
				$this->highlights[] = new Video($n['id']);
			}
		}
	}
	
	public function findMVP() {
		$blr = 0;
		$mvp = NULL;
		$pobednik = NULL;
						
		if ($this->domacin->stats->pts > $this->gost->stats->pts) {
			$pobednik = clone $this->domacin;
		}
		else {
			$pobednik = clone $this->gost;
		}
		
		foreach($pobednik->roster as $igrac) {
			$igrac->stats[$this->id]->BLindex = $igrac->stats[$this->id]->calculateBLindex();
			if ($igrac->stats[$this->id]->BLindex > $blr) {
				$mvp = $igrac;
				$blr = $igrac->stats[$this->id]->BLindex;
			}
		}
		
		return $mvp;
	}
	
	public function FindTopScorer() {
		$topScorer = NULL;
		$this->domacin->getRoster();
		$this->gost->getRoster();
		
		$max = 0;
		foreach($this->domacin->roster as $igrac) {
			$igrac->getStats($this->id);
			if ($igrac->stats[$this->id]->pts > $max) {
				$max = $igrac->stats[$this->id]->pts;
				$topScorer = clone $igrac;
			}
		}
		foreach($this->gost->roster as $igrac) {
			$igrac->getStats($this->id);
			if ($igrac->stats[$this->id]->pts > $max) {
				$max = $igrac->stats[$this->id]->pts;
				$topScorer = clone $igrac;
			}
		}
		
		return $topScorer;
	}
	
	public function isWinner($team) {
		$w = NULL;
		
		$helpMatch = new Match($this->id);
		$helpMatch->domacin->stats = $helpMatch->domacin->getMatchStats($this->id);
		$helpMatch->gost->stats = $helpMatch->gost->getMatchStats($this->id);
				
		if ($helpMatch->domacin->stats->pts > $helpMatch->gost->stats->pts) {
			$w = $helpMatch->domacin->id;
		}
		else {
			$w = $helpMatch->gost->id;
		}
		
		if ($team->id == $w) return 1;
		else return 0;
	}
	
	public function getWinner(){ //pomocna funkcija za prediction
		if ($this->isWinner($this->domacin))
			return 1;
		else
			return 2;
	}
	
	
	public function lockPrediction() { //pomocna funkcija za prediction, proverava kada pocinje mec
		$trenutni_datum = strtotime('NOW');
		$pom_m_time = strtotime($this->vreme);
		$trenutno_time = strtotime(date("H:i", strtotime('NOW')));
		
		$pom_kolo = new Round($this->kolo);
		if ($trenutni_datum < strtotime($pom_kolo->datum)){
			return 0;
		}
		elseif($trenutni_datum == strtotime($pom_kolo->datum)){
			if ($pom_m_time - $trenutno_time > 3600){
				return 0;
			}
			else {
				return 1;
			}
		}
		else{
			return 1; // za veliki if
		}
	}
	 
	// admin
	
	public function insertIntoDb() {
		$q = "INSERT INTO utakmica (liga, kolo, domacin, gost, vreme, odigrana) VALUES ('".$this->liga."', '".$this->kolo."', '".$this->domacin->id."', '".$this->gost->id."', '".$this->vreme."', '".$this->odigrana."')";
		$r = mysql_query("set names 'utf8'");
		if (mysql_query($q)) {
			echo "Novi mec je dodat u bazu podataka";
		}
	}
	
	public function startMatch() {
		if ($this->odigrana) throw new Exception("Match has already started!");
		$this->odigrana = 1;
		$q = "UPDATE utakmica SET odigrana = 1 WHERE id = ".$this->id;
		mysql_query($q);
		foreach($this->domacin->roster as $player) {
			$s = new Statistics();
			$s->igrac = $player->id;
			$s->mec = $this->id;
			$s->insertIntoDb();
		}
		foreach($this->gost->roster as $player) {
			$s = new Statistics();
			$s->igrac = $player->id;
			$s->mec = $this->id;
			$s->insertIntoDb();
		}
	}
			
} // end of class Match


?>